﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.BusinessLogic.Entities;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.UI.Common.Events;
using DiscoveryLogic.Common.Utilities;
using Microsoft.Practices.Composite.Events;
using EntityBase=DiscoveryLogic.Analytics.DataObjects.EntityBase;

namespace DiscoveryLogic.Analytics.Modules.Entities
{
    public class PersonTreePresenter
    {
        private PersonTreeView _View;
		/// <summary></summary>
		public PersonTreeView View
		{
			get
			{
				return this._View;
			}
			set
			{
				this._View = value;
				if(this._View !=null)
				{
					this._View.PersonDataSetSelected += new PersonTreeView.OnPersonDataSetSelected(SelectPersonDataSet);
				    this._View.PopulateTreeView(this.ReadPersonDataSets());
				}
			}
		}

		private readonly IEventAggregator _EventAggregator;

        public PersonTreePresenter(IEventAggregator eventAggregator)
		{
			this._EventAggregator = eventAggregator;
		}

		public void SelectPerson(PersonDO person)
		{
            if (person != null)
			{
				this._EventAggregator.GetEvent<PersonSelectedEvent>().Publish(person);
			}
		}

        private void SelectPersonDataSet(string dataSetName)
        {
            if (!string.IsNullOrEmpty(dataSetName))
            {
                this._EventAggregator.GetEvent<PersonDataSetSelectedEvent>().Publish(dataSetName);
            }
        }

        private List<string> ReadPersonDataSets()
		{
		    List<string> personDataSetNames=new List<string>();
            Dictionary<string, List<EntityBase>> grantDataSets = EntityManager.GetEntityDataSets(EntityType.Persons);
            if(grantDataSets !=null && grantDataSets.Count>0)
            {
                personDataSetNames.AddRange(grantDataSets.Keys);
            }
            return personDataSetNames;
		}
    }
}
